package org.restly.servlet;

import org.restly.StylesheetLocator;
import org.restly.HttpStatus;

import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

/**
 * Oct 26, 2008, 10:09:46 PM
 *
 * @author <a href="http://restly.org/">Erik Beeson</a>
 */
public class RestlyContext {
	private final ServletContext servletContext;
	private final StylesheetLocator stylesheetLocator;

	public RestlyContext(FilterConfig config) throws ServletException {
		this.servletContext = config.getServletContext();

		org.restly.StylesheetLocator stylesheetLocator = null;

		String stylesheetLocatorClassName = config.getInitParameter(StylesheetLocator.class.getName());

		if(stylesheetLocatorClassName != null) {
			try {
				stylesheetLocator = (StylesheetLocator) Class.forName(stylesheetLocatorClassName).newInstance();
			} catch(Exception e) {
				throw new ServletException("Problem creating StylesheetLocator", e);
			}
		}

		if(stylesheetLocator == null) {
			stylesheetLocator = new StylesheetLocator() {
				public String getStylesheetUrl(RestlyHttpServletRequest request, HttpStatus status, String stylesheet) {
					return null;
				}
			};
		}

		this.stylesheetLocator = stylesheetLocator;
	}

	public ServletContext getServletContext() {
		return servletContext;
	}

	public StylesheetLocator getStylesheetLocator() {
		return stylesheetLocator;
	}
}
